<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class DrugOrderService extends BaseService
{
    //药品订单

    /***
     * 列表
     * @param array $pages【start、limit】
     */
    public function getIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbDrugOrder)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback();
        }
        $statuss = $this->drug_statuss;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //药品
            $drugOne = (array) $this->dbConn($this->dbDrug)
                ->where('id', $v['drug_id'])
                ->first();
            $drug_name = !empty($drugOne)&&$drugOne['title'] ? $drugOne['title'] : '-';
            //用户
            $uOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $v['uid'])
                ->first();
            $uname = !empty($uOne)&&$uOne['uname'] ? $uOne['uname'] : '-';
            //收货人
            $acceptOne = (array) $this->dbConn($this->dbUaddress)
                ->where('id', $v['address_id'])
                ->first();
            $accepter = !empty($acceptOne)&&$acceptOne['accepter'] ? $acceptOne['accepter'] : '-';
            $acceptDoor = !empty($acceptOne)&&$acceptOne['map_name'] ? $acceptOne['map_name'] : '-';
            $status_name = array_key_exists($v['status'], $statuss) ? $statuss[$v['status']] : '';
            $arys[] = [
                'id' => $v['id'],
                'title' => $drug_name,
                'uname' => $uname,
                'price' => toNumber($v['price']),
                'num' => $v['num'],
                'accepter' => $accepter,
                'acceptDoor' => $acceptDoor,
                'pstime' => $v['pstime'],
                'status' => $v['status'],
                'status_name' => $status_name,
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /***
     * 发货
     * @param int $id
     */
    public function send($id)
    {
        $res = (array) $this->dbConn($this->dbDrugOrder)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        if ($res['status'] < 2) {
            return $this->toback(-2, '未支付');
        }
        if ($res['status'] == 3) {
            return $this->toback(-2, '已发货');
        }
        if ($res['status'] == 4) {
            return $this->toback(-2, '已完成');
        }
        $up = $this->dbConn($this->dbDrugOrder)
            ->where('id', $id)
            ->update(['status' => 3, 'sendtime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /***
     * 完成
     * @param int $id
     */
    public function finish($id)
    {
        $res = (array) $this->dbConn($this->dbDrugOrder)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        if ($res['status'] < 3) {
            return $this->toback(-2, '请选支付或发货');
        }
        if ($res['status'] == 4) {
            return $this->toback(-2, '已完成');
        }
        $up = $this->dbConn($this->dbDrugOrder)
            ->where('id', $id)
            ->update(['status' => 4, 'finishtime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }
}